// 统一封装接口方法
// 每个方法负责请求一个url地址
// 逻辑页面，导入这个接口方法就能发请求
// 好处：请求url路径，可以在这里统一管理
import request from '../utils/request'

// 频道 - 获取所有频道
export const getAllChannelListAPI = () => {
  return request({
    url: '/v1_0/channels'
  })
}

export const loginAPI = ({ mobile, code }) => {
  return request({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: {
      mobile,
      code
    }
  })
}

export const userChannelAPI = () => request({
  url: '/v1_0/user/channels'

})

export const articlesListAPI = ({ channelId, timestamp }) => request({
  url: '/v1_0/articles',
  params: {
    channel_id: channelId,
    timestamp
  }
})

// 文章反馈 - 不感兴趣
export const dislikeArticleAPI = ({ artId }) => request({
  url: '/v1_0/article/dislikes',
  method: 'POST',

  data: {
    target: artId
  }
})

// 文章 - 反馈 - 垃圾内容
export const reportArticleAPI = ({ artId, type }) => request({
  url: '/v1_0/article/reports',
  method: 'POST',

  data: {
    target: artId,
    type,
    remark: '如果点击数0,弹出一个输入框输入其他问题传参到这里'
  }
})

// 频道 - 更新已选
export const updateChannelAPI = ({ channels }) => request({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: {
    channels
  }
})

// 频道 - 删除用户指定频道
export const removeTheVhannelAPI = ({ channelId }) => request({
  url: `/v1_0/user/channels/${channelId}`,
  method: 'DELETE'
})

// 搜索 - 联想菜单
export const suggestListAPI = ({ keywords }) => request({
  url: '/v1_0/suggestion',
  params: {
    q: keywords
  }
})

export const searchResultListAPI = ({ q, page }) => request({
  url: '/v1_0/search',
  params: {
    q,
    page
  }
})

// 文章 - 详情
export const articleDetailAPI = ({ id }) => {
  return request({
    // :id是后台规定的参数名
    // 前端要在对应路径位置传值(不要写:)
    url: `/v1_0/articles/${id}`
  })
}

// 文章 - 关注作者
export const followedUserAPI = ({ uid }) => {
  return request({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target: uid
    }
  })
}
// 文章 - 取消关注作者
export const unFollowedUserAPI = ({ uid }) => {
  return request({
    // 这uid只是个变量名, 把值拼接在url发给后台(无需指定参数名)
    url: `/v1_0/user/followings/${uid}`,
    method: 'DELETE'
  })
}

// 文章 - 点赞
export const articleLikeAPI = ({ aid }) => {
  return request({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target: aid
    }
  })
}

// 文章 - 取消点赞
export const articleDisLikeAPI = ({ aid }) => {
  return request({
    url: `/v1_0/article/likings/${aid}`,
    method: 'DELETE'
  })
}

// 评论 - 获取列表
// offset第一页时, 不用在params携带(axios发现值为null会自动忽略此参数)
export const commentListAPI = ({ artId, offset = null, limit = 10 }) => {
  return request({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source: artId,
      offset,
      limit
    }
  })
}

// 评论 - 喜欢
export const commentLikingAPI = ({ comId }) => {
  return request({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: comId
    }
  })
}
// 评论-取消喜欢
export const commentDisLikingAPI = ({ comId }) => {
  return request({
    url: `/v1_0/comment/likings/${comId}`,
    method: 'DELETE'
  })
}

// 评论 - 发布
export const sendCommentAPI = ({ artId, content }) => {
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: {
      target: artId,
      content: content
    }
  })
}

// 获取个人资料
export const userProfileAPI = () => request({
  url: '/v1_0/user/profile'
})

// 获取基本信息（我的页面显示数据）
export const userAPI = () => request({
  url: '/v1_0/user'
})

// 用户 - 更新头像
// 注意: formObj的值必须是一个表单对象
// '{"a": 10, "b": 20}' // 对象格式的JSON字符串
// new FormData() // 表单对象
export const updatePhotoAPI = (fd) => request({
  url: '/v1_0/user/photo',
  method: 'PATCH',
  data: fd
})

// 用户 - 更新资料
export const updateProfileAPI = ({ birthday, userName }) => {
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: {
      birthday: birthday,
      name: userName
    }
  })
}

// 用户 - 刷新 token
export const refreshTokenAPI = () => request({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    Authorization: 'Bearer ' + localStorage.getItem('refresh_token')
  }
})
